package com.mobilepay.moneypig.model.activity.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.base.NoDoubleClickListener;
import com.mobilepay.moneypig.config.Key;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;
import com.mobilepay.moneypig.utils.PreferenceUtil;

/**
 * Created by xlm on 2017/7/27 0027
 */

public class ChangePassWord extends BaseActivity {
    private static final String TAG = "ChangePassWord";
    private RelativeLayout mRelativeBack;
    private EditText mEditold;
    private EditText mEditnew;
    private EditText mEditnewTwo;
    private Button mButtonChange;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_change_password);
    }

    @Override
    protected void initViews() {
        mRelativeBack = (RelativeLayout) findViewById(R.id.change_back);
        mEditold = (EditText) findViewById(R.id.change_old);
        mEditnew = (EditText) findViewById(R.id.change_new);
        mEditnewTwo = (EditText) findViewById(R.id.change_new_two);
        mButtonChange = (Button) findViewById(R.id.change_button);
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {

    }

    @Override
    protected void onSetListener() {
        mRelativeBack.setOnClickListener(this);
        mButtonChange.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                super.onNoDoubleClick(view);
                checkContent();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.change_back:
                finish();
                break;
        }
    }

    String password;
    String newPassword;

    private void checkContent() {
        password = (String) PreferenceUtil.getParam(getThis(), Key.PASSWORD, "");
        newPassword = mEditnew.getText().toString();
        String old = mEditold.getText().toString();
        String newCommit = mEditnewTwo.getText().toString();
        if (CommonUtil.isEmpty(password)) {
            return;
        }
        if (CommonUtil.isEmpty(old)) {
            showToastShort("请输入旧密码");
        } else if (CommonUtil.isEmpty(newPassword)) {
            showToastShort("请输入新密码");
        } else if (CommonUtil.isEmpty(newCommit)) {
            showToastShort("请再次输入新密码");
        } else {
            if (password.endsWith(old)) {
                if (newPassword.endsWith(newCommit)) {
                    if (newPassword.endsWith(password)) {
                        showToastShort("新旧密码不能相同");
                        mEditnew.setText(null);
                        mEditnewTwo.setText(null);
                    } else {
                        changePassword();//修改密码
                    }

                } else {
                    showToastShort("新密码输入不一致,请重新输入");
                    mEditnew.setText(null);
                }
            } else {
                showToastShort("旧密码错误,请重新输入");
                mEditold.setText(null);
            }
        }
    }

    private void changePassword() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.change_password);
        param.add("uid", String.valueOf(App.getApp().getUserInfo().getData().getUid()));
        param.add("password", password);
        param.add("newpassword", newPassword);
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                if (CommonUtil.isForeground(getThis())) {
                    showToastShort(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
                    showToastShort(responseResult.getInfo());
                    if (responseResult.getStatus() == 1) {
                        //更新本地密码
                        updatePassword(newPassword);
                    }
                }
            }
        });
    }

    //更新本地密码
    private void updatePassword(String password) {
        if (CommonUtil.isEmpty(password)) {
            return;
        }
        PreferenceUtil.setParam(getThis(), Key.PASSWORD, password);
        finishActivity(true);
    }
}
